﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32;
using MyDefaults.Core;

namespace MyDefaults
{
    public partial class SplashScreen : Form
    {
        private readonly Updater _updater = new Updater();
        private readonly StringBuilder _consoleText = new StringBuilder();

        private const string RunKeyValueName = "MyDefaults";

        public SplashScreen()
        {
            InitializeComponent();
        }

        private void SplashScreen_Shown(object sender, EventArgs e)
        {
            AppendConsoleText("Initializing...");
            StartUpdater();
            AppendConsoleText("Ready.");
            new Thread(WaitThenHideWindow).Start();
        }

        private void StartUpdater()
        {
            _updater.Start();
            startToolStripMenuItem.Visible = false;
            stopToolStripMenuItem.Visible = true;
        }

        private void WaitThenHideWindow()
        {
            Thread.Sleep(1000);
            AppendConsoleText("Running in background.");
            Thread.Sleep(500);
            HideWindow();
        }

        private void HideWindow()
        {
            if (InvokeRequired)
            {
                Invoke(new Action(HideWindow));
                return;
            }

            Hide();
        }

        

        private void AppendConsoleText(string text)
        {
            if (InvokeRequired)
            {
                Invoke(new Action<string>(AppendConsoleText), text);
                return;
            }

            _consoleText.AppendLine(text);
            textBox1.Text = _consoleText.ToString();
        }

        private void SplashScreen_FormClosing(object sender, FormClosingEventArgs e)
        {
            StopUpdater();
        }

        private void StopUpdater()
        {
            _updater.Stop();
            startToolStripMenuItem.Visible = true;
            stopToolStripMenuItem.Visible = false;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private bool AutoRun
        {
            get
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                return key.GetValue(RunKeyValueName) != null;
            }
            set
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                if (key != null)
                {
                    if (value && key.GetValue(RunKeyValueName) == null)
                    {
                        string programLocation = Assembly.GetAssembly(GetType()).Location;
                        key.SetValue(RunKeyValueName, programLocation, RegistryValueKind.String);
                    }
                    else if (key.GetValue(RunKeyValueName) != null)
                    {
                        key.DeleteValue(RunKeyValueName);
                    }
                }
            }
        }

        private void SplashScreen_Load(object sender, EventArgs e)
        {
            if (AutoRun)
            {
                startAutomaticallyToolStripMenuItem.Checked = true;
            }
        }

        private void startAutomaticallyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (((ToolStripMenuItem)sender).Checked) // then disable aurorun
            {
                AutoRun = true;

            }
            else // enable autorun
            {
                AutoRun = false;
            }
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StartUpdater();
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StopUpdater();
        }
    }
}
